#include "ACos.h"
long double ACos::calcul(long double aux1)
{
    if (aux1 < -1 || aux1 > 1)
    {
        std::cout<<"Arccos e definit intre [-1..1]";
        int sgn = aux1 < 0 ? -1 : 1;

        while (sgn * aux1 > sgn * 1)
            aux1 -= sgn * 2;
    }
    return acosl(aux1);
}

long double ACos::calcul()
{
    if (!fStang)
        exit(1);
    return calcul(fStang->calcul());
}

char *ACos::derivare()
{/*d(arccos(u))/dx = -du/dx / sqrt(1 - u * u)*/
    char *aux1, *aux2, *aux, *aux00, *aux01, *aux02, *aux03;

    if (!fStang)
        exit(1);

    aux1 = fStang->derivare();
    aux2 = fStang->toString();

    aux00 = Putere::concatTermeni(aux2, "2");/*u^2*/
    aux01 = Minus::concatTermeni("1", aux00);/*1-u^2*/
    aux02 = Putere::concatTermeni(aux01, "0.5");/*sqrt(1-u^2)*/
    aux03 = Impartit::concatTermeni(aux1, aux02);/*du/dx/sqrt(1-u^2)*/
    aux = Minus::concatTermeni("0", aux03);/*-du/dx/sqrt(1-u^2)*/
    delete aux1; delete aux2; delete aux00; delete aux01; delete aux02; delete aux03;

    return aux;
}

long double ACos::calculDerivata()
{
    if (!fStang)
        exit(1);

    return Minus::calcul(0, Impartit::calcul(fStang->calculDerivata(), Putere::calcul(
            Minus::calcul(1, Putere::calcul(fStang->calcul(), 2)), 0.5)));
}

char *ACos::toString()
{
    char *aux, *aux1;
    int nrP;

    if (!fStang)
        exit(1);
    aux1 = fStang->toString();

    aux = concatTermeni(aux1);
    delete aux1;
    return aux;
}

char *ACos::concatTermeni(char *aux1)
{
    char *aux;
    int nrP;

    nrP = aux1[0] == '(' ? 0 : 2;
    aux = new char[strlen(aux1) + 5 + nrP];

    strcpy(aux, "acos");
    if (nrP) strcat(aux, "(");
    strcat(aux, aux1);
    if (nrP) strcat(aux, ")");

    return aux;
}
